package com.edu.compile.template.ftl;


import com.edu.compile.ParserHolder;
import com.edu.compile.template.AbstractNodeTemplateParse;
import com.edu.compile.template.NodeTemplateParse;
import com.edu.compile.template.TemplatePolymerization;
import com.edu.compile.template.buffer.Buffer;
import com.edu.compile.template.ftl.call.LoopCall;
import com.edu.compile.template.ftl.call.SimpleCall;
import com.edu.model.tree.v2.TreeNode;

import java.util.Iterator;
import java.util.List;

/**
 * 通用的节点模板
 *
 * @author yanzhao
 * @date 2021/10/20 23:45
 * @since 1.0.0
 */
public class CommonNodeTemplateParse extends AbstractNodeTemplateParse {

    public CommonNodeTemplateParse(String nodeId, String nodeType, String nodeName, String parentNodeId) {
        super(nodeId, nodeType, nodeName, parentNodeId);
    }

    public String parseMethodCall(TemplatePolymerization handler, TreeNode<NodeTemplateParse> treeNode, Buffer buffer) {
        List<TreeNode<NodeTemplateParse>> childrenNode = treeNode.getChildrenNode();
        Iterator<TreeNode<NodeTemplateParse>> iterator = childrenNode.iterator();
        if (this.isLoop()) {
            buffer.append(ParserHolder.templateParser.parse(new LoopCall(this.getNodeId())));
        } else {
            buffer.append(ParserHolder.templateParser.parse(new SimpleCall(this.getNodeId())));
        }
        while (iterator.hasNext()) {
            TreeNode<NodeTemplateParse> next = iterator.next();
            NodeTemplateParse data = next.getData();
            data.parse(handler, next, buffer);
        }
        return null;
    }

}
